package app.controller.msgmanage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import app.utils.DataResponse;

/**
 * 这个类用于被调用用来传输特定时间之前转发失败过的数据，用来重新转发
 * @author 昊琦
 * @创建时间 2018年3月19日16:53:59
 */
@Controller
public class MsgManageAgain {


    @RequestMapping(value="/msgmanageagain",method=RequestMethod.POST)
    public void msgManageAgain(HttpServletRequest request,HttpServletResponse response){

        String reponseMsg ="";

        //用于接收主服务器发送来的消息
        String mess = request.getHeader("mess");
        //用于接收主服务器发送来的消息的时间
        String timeNow = request.getHeader("d_time");

        long d_time = 0L;

        if (timeNow!=null&&!timeNow.replace(" ", "").equals("")&&!timeNow.replace(" ", "").equals("null")) {
            d_time = Long.valueOf(timeNow);
        }

        System.out.println("执行到MsgManageAgain");

        System.out.println(mess);
        System.out.println(d_time);

        //判断消息的有效性
        if (mess==null||mess.equals("")) {

            System.out.println("没有收到有效数据");
            reponseMsg="0";

            //返回数据
            DataResponse.jsonResponse(response, reponseMsg);
            return;
        }

        //正则表达式校验
        if (devCodeCheck(mess)==false) {
            System.out.println("正则表达式匹配不通过");
            reponseMsg="0";
            //返回数据
            DataResponse.jsonResponse(response, reponseMsg);
            return;
        }


        //去括号
        mess = mess.substring(1, mess.length()-1);
        //按逗号做分割
        String[] msgArray = mess.split(",");


        //根据消息中的控制类型码做相应处理
        switch (msgArray[1]) {
            //硬件主动上报的故障信息
            case "98":

                reponseMsg =new MsgManageSupportAgain().faultHandle(msgArray,d_time);

                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器读取心跳参数
            case "51":

                new MsgManageSupportAgain().heartbeatHandle(msgArray,d_time);
                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器设置心跳参数
            case "53":

                new MsgManageSupportAgain().deviceRespondToChangeHeartbeat(msgArray,d_time);
                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器读取等待用户开锁时间参数
            case "55":

                new MsgManageSupportAgain().deviceRespondToServerRequestLockTime(msgArray, d_time);

                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器设置等待用户开锁时间参数
            case "57":

                new MsgManageSupportAgain().deviceRespondToServerSetLockTime(msgArray, d_time);

                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器读取等待用户开门时间参数
            case "59":

                new MsgManageSupportAgain().deviceRespondToServerRequestDoorTime(msgArray, d_time);

                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器设置等待用户开门时间参数
            case "61":

                new MsgManageSupportAgain().deviceRespondToServerSetDoorTime(msgArray, d_time);

                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应服务器设置服务器地址及端口
            case "63":
                new MsgManageSupportAgain().deviceRespondToChangeIp(msgArray, d_time);
                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件向备用服务器主动报告与主服务器通信异常
            case "64":
                new MsgManageSupportAgain().deviceReportCommunicationsError(msgArray, d_time);
                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件主动上报心跳
            case "00":

                new MsgManageSupportAgain().receiveHeartbeat(msgArray, d_time);
                reponseMsg ="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //地井硬件主动上报地井井盖状态变化信息
//		case "04":
//
//			reponseMsg = new MsgManageSupportAgain().coverReportStu(msgArray, d_time);
//			return "data";
            //通断电设备主动上报通断电状态信息，地井主动上报井盖状态变化信息
            case "06":

                new MsgManageSupportAgain().onoffDeviceReportStu(msgArray, d_time);
                reponseMsg ="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件响应完成机房开门
            case "09":

                new MsgManageSupportAgain().deviceRespondToFinishOpenDoor(msgArray, d_time);
                reponseMsg ="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //机柜锁硬件主动上报电量信息
            case "10":

                new MsgManageSupportAgain().lockReportElectricQuantity(msgArray, d_time);
                reponseMsg = "0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件主动上报机柜锁按键触发开门请求
            case "12":

                new MsgManageSupportAgain().lockDeviceRequestOpenDoor(msgArray, d_time);
                reponseMsg = "0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //硬件主动上报机柜锁和门状态发生变化
            case "14":

                new MsgManageSupportAgain().lockDeviceReportLockAndDoorStu(msgArray, d_time);
                reponseMsg ="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
            //用户未执行开门，硬件主动上报
            case "16":

                new MsgManageSupportAgain().deviceReportUserNotOpenDoor(msgArray, d_time);
                reponseMsg = "0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;

            default:
                reponseMsg="0";
                //返回数据
                DataResponse.jsonResponse(response, reponseMsg);
                break;
        }

    }

    /**
     * 这个方法被用于校验接收到的硬件上行消息数据格式是否是包含[设备编码,控制类型码] 格式的
     * @param devCode
     * @return
     */
    private Boolean devCodeCheck(String devCode){

        String reg = "^\\[[A-Z]{1}[0-9]{5},\\d\\d\\S*\\]$";

        Pattern pattern = Pattern.compile(reg);

        Matcher matcher = pattern.matcher(devCode);
        return matcher.matches();
    }
}
